package net.mikoo.seals.biz.manager;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.mikoo.seals.biz.mapper.WorkflowMapper;
import net.mikoo.seals.common.enums.WorkflowStatusEnum;
import net.mikoo.seals.common.enums.WorkflowTypeEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.model.Workflow;
import net.mikoo.seals.model.WorkflowSetting;

@Component
public class WorkflowManager {
	
	@Autowired
	private WorkflowMapper workflowMapper;
	

	public void change(Workflow workflow) {

		Integer total = workflowMapper.countUsedTotalById(workflow.getWorkflowId());
		if (total == null || total == 0) {
			updated(workflow);
		} else {

			created(workflow);
		}
	}

	public void created(Workflow workflow) {

		workflow.setWorkflowId(null);
		workflow.setParentId(workflow.getWorkflowId());
		workflow.setWorkflowStatus(WorkflowStatusEnum.PENDING.code());
		workflow.setIsEnable("Y");
		workflow.setCreateTime(new Date());
		workflowMapper.insert(workflow);

		List<WorkflowSetting> settings = workflow.getSettings();
		if (settings == null || settings.isEmpty()) {
			throw new ParametersException("请添加审核人员");
		}

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < settings.size(); i++) {
			WorkflowSetting setting = settings.get(0);

			if (workflow.getWorkflowType().equals(WorkflowTypeEnum.QUEUE.code())) {
				if (setting.getSequence() == null) {
					throw new ParametersException("请设置审核人员顺序");
				}
			} else {
				if (setting.getSequence() == null) {
					setting.setSequence(0);
				}
			}

			setting.setWorkflowId(workflow.getWorkflowId());
			workflowMapper.insertSetting(setting);

			if (i == 0) {
				sb.append(",");
			}
			sb.append(setting.getAuditor());
		}
		workflow.setAuditor(sb.toString());
		workflowMapper.update(workflow);

	}

	public void updated(Workflow workflow) {

		workflow.setWorkflowStatus(WorkflowStatusEnum.PENDING.code());
		workflowMapper.update(workflow);

		List<WorkflowSetting> settings = workflow.getSettings();
		if (settings == null || settings.isEmpty()) {
			throw new ParametersException("请添加审核人员");
		}

		workflowMapper.deleteSettingByWorkflowId(workflow.getWorkflowId());
		
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < settings.size(); i++) {
			WorkflowSetting setting = settings.get(i);

			if (workflow.getWorkflowType().equals(WorkflowTypeEnum.QUEUE.code())) {
				if (setting.getSequence() == null) {
					throw new ParametersException("请设置审核人员顺序");
				}
			} else {
				if (setting.getSequence() == null) {
					setting.setSequence(0);
				}
			}

			setting.setWorkflowId(workflow.getWorkflowId());
			workflowMapper.insertSetting(setting);

			if (i == 0) {
				sb.append(",");
			}
			sb.append(setting.getAuditor());
		}
		workflow.setAuditor(sb.toString());
		workflowMapper.update(workflow);

	}

	public Workflow analyze(Seal seal) {
		
		String verifyPartyIds = seal.getVerifyPartyIds();
		String workflowType = seal.getWorkflowType();
		Long companyId = seal.getCompanyId();

		if (StringUtils.isBlank(verifyPartyIds)) {
			throw new ParametersException("参数传入有误");
		}

		Workflow workflow = new Workflow();
		workflow.setWorkflowType(workflowType);
		workflow.setWorkflowStatus(WorkflowStatusEnum.RELEASE.code());
		workflow.setIsEnable("Y");
		workflow.setCreateTime(new Date());
		workflow.setCompanyId(companyId);
		workflow.setParentId(0L);
		workflow.setSettingStr(verifyPartyIds);
		workflow.setName("审核流程");
		if(seal.getWorkflowId() == null) {
			workflowMapper.insert(workflow);
			workflow.setWorkflowId(workflow.getWorkflowId());
		}else {
			workflow.setWorkflowId(seal.getWorkflowId());
			workflowMapper.deleteSettingByWorkflowId(seal.getWorkflowId());
		}
		
		List<WorkflowSetting> settings = workflow.getSettings();
		for (WorkflowSetting setting : settings) {
			setting.setWorkflowId(workflow.getWorkflowId());
			workflowMapper.insertSetting(setting);
		}

		return workflow;
		
	}
	
	public Workflow special(Seal seal) {

		if(seal.getSpecialWorkflowId() !=null) {
			workflowMapper.deleteSettingByWorkflowId(seal.getSpecialWorkflowId());
		}
		
		if (seal.getUseSpecial().equals("Y")) {
			
			if(seal.getSpecialPartyId()==null) {
				throw new ParametersException("请选择特批审核人员");
			}
			
			Long companyId = seal.getCompanyId();
		
			Workflow workflow = new Workflow();
			workflow.setWorkflowType(WorkflowTypeEnum.SPECIAL.code());
			workflow.setWorkflowStatus(WorkflowStatusEnum.RELEASE.code());
			workflow.setIsEnable("Y");
			workflow.setCreateTime(new Date());
			workflow.setCompanyId(companyId);
			workflow.setParentId(0L);
			workflow.setName("特批流程");
			
			if(seal.getSpecialWorkflowId() == null) {
				workflowMapper.insert(workflow);
			}else {
				workflow.setWorkflowId(seal.getSpecialWorkflowId());
			}
			
			
			WorkflowSetting setting = new WorkflowSetting();
			setting.setPartyId(seal.getSpecialPartyId());
			setting.setWorkflowId(workflow.getWorkflowId());
			setting.setSequence(0);
			workflowMapper.insertSetting(setting);
			
			return workflow;

		}

		return null;

	}

}
